computeIRR

fun computeIRR(seedRate: Double = 0.1, strategies: List<IRR.SolveStrategy> = listOf(SolveStrategy.secant), reverseSeedOnFail: Boolean = false, clearResults: Boolean = true): List<IRR.CalculationResult>

Computes an annualised Internal Rate of Return (IRR), sometimes referred to as the Return on Investment (ROI), for a series of irregular cash flows. Supports multiple root-solving strategies.

The cached list of calculated results is automatically cleared every time this function is called.

Supported IRR Solving Strategies

SolveStrategy.secant Secant Method:

Iterates until the net present value (NPV) is within 0.00001 (≈ 0.001%) or a maximum of 100 iterations is reached.

Formula:

Rₙ₊₁ = Rₙ - NPVₙ * ((Rₙ - Rₙ₋₁) / (NPVₙ - NPVₙ₋₁))

Sensitive to the initial seedRate. If convergence fails, the method retries with -seedRate (when enabled).

SolveStrategy.newton_raphson Newton-Raphson Method:

Uses the derivative of the NPV function to find the root. Typically converges faster when the derivative is stable.

Formula:

Rₙ₊₁ = Rₙ - (NPVₙ / NPV′ₙ)

Can fail if the derivative is near zero or the initial guess is poor. When enabled, the method retries with -seedRate.

SolveStrategy.bisection Bisection Method:

Guaranteed to converge if the NPV changes sign over the interval. Slower but reliable fallback.

At each step:

Rₙ = (aₙ + bₙ) / 2

where [aₙ, bₙ] brackets the root and NPV(aₙ) and NPV(bₙ) have opposite signs.

Bracket: [-0.9999, 1e10]. If the initial bracket fails, the method may retry using a reversed bracket.

Accepted Results

A strategy only reports a solution when the rate is a real number above -100% and the NPV is within its tolerance. The tolerance grows with the size of the cash flows (see NPV_SCALE_FACTOR), as very large amounts cannot be netted down to a fixed tiny amount.

Strategy Fallback Logic

The caller provides a list of one of more IRR solving strategies via strategies. These are processed in the given order. For each strategy:

  • The function first attempts with seedRate.

  • If reverseSeedOnFail is true, it retries with -seedRate (if applicable).

  • If the strategy still fails, the next one is attempted (if any).

  • The process continues until a valid solution is found or all strategies fail.

Only a single root is returned — the first successfully converged solution.

Excel XIRR Compatibility

This implementation aims to closely match Excel's XIRR() function when you call with:

strategies = listOf(newton_raphson, bisection, secant)
reverseSeedOnFail = true
  • Using a tolerance of 1e-8,

  • Limiting to 100 iterations,

  • Falling back across multiple strategies,

  • Using a wide bisection bracket: [-0.9999, 1e10].

Sign Convention

Moneydance uses a standard financial convention:

  • Positive values = cash outflows (e.g. investments),

  • Negative values = cash inflows (e.g. proceeds), This aligns with discounted cash flow conventions and does not affect result validity.

Notes

  • Returns Double.NaN if no valid root is found.

  • Seed choice significantly affects convergence with Newton-Raphson and Secant.

  • The result is an annualised rate, so a gain made over a short time, or on a very small amount invested, produces a very large - but correct - IRR.

Return

A list of CalculationResult result objects generated by this calculation. Use getResults to retrieve all results stored within this IRR object. This function is guaranteed to return a list containing at least one entry - even if the IRR could not be solved (e.g., NaN result for invalid cash flows).

Since

MD2027

Parameters

seedRate

Initial guess for the IRR (default = 0.1 for 10%) - This will be ignored when / if Bisection is called.

strategies

List of IRR solving strategies to try (in order). Must be non-empty. Default is SolveStrategy.secant.

reverseSeedOnFail

If true, will retry with -seedRate before falling back to the next strategy. Default is false.

clearResults

When specified, then the results table is cleared before any calculations are performed - i.e. start with a clean slate. Otherwise new results will be appended to the list. It's therefore possible to have multiple solved IRR computations.

See also